package com.lyfx.reptiles.utils;

import cn.hutool.core.text.UnicodeUtil;
import com.lyfx.common.pojo.CoursePojo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lsy
 * @create 2020-08-01-17:41
 */
public class CourseUtil {


    public static List<CoursePojo> catPojoList(String first,String course,Integer page) throws Exception {
        String url = "https://www.shiyanlou.com/search/?type=course&search="+course+"&page_size=15&page="+page;
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements select = document.select("script");
        String html = select.html();
        String result = html.split("results:")[2];
        result = result.split("mutation-types")[0];
        String[] ids = result.split("\\{id");
        List<CoursePojo> list = new ArrayList<>();
        for (int i = 1; i < ids.length; i++) {
            CoursePojo coursePojo = new CoursePojo();
            String name = ids[i].split("name:")[1].split("picture_url")[0].replace("\"", "").replace(",", "").trim();
            String picture_url = ids[i].split("picture_url:")[1].split("description")[0].replace("\"", "").replace(",", "").trim();
            String description = ids[i].split("description:")[1].split("students_count")[0].replace("\"", "").trim();
            description = description.substring(0,description.length()-1);
            String students_count = ids[i].split("students_count:")[1].split("fee_type")[0].replace("\"", "").replace(",", "").trim();
            coursePojo.setSort(0);
            coursePojo.setName(name);
            coursePojo.setCourseHeat(Integer.parseInt(students_count));
            coursePojo.setDescript(description);
            coursePojo.setFirstLetter(first);
            coursePojo.setLogo(UnicodeUtil.toString(picture_url));
            list.add(coursePojo);
        }
        return list;
    }



}
